package ar.uade.edu.fain.ad.server.dao.jpa;

import java.util.List;

import javax.persistence.PersistenceException;

import org.hibernate.Session;

import ar.uade.edu.fain.ad.server.business.Administracion;
import ar.uade.edu.fain.ad.server.business.Administrador;
import ar.uade.edu.fain.ad.server.business.Caja;
import ar.uade.edu.fain.ad.server.business.Cajero;
import ar.uade.edu.fain.ad.server.business.Empleado;
import ar.uade.edu.fain.ad.server.business.Local;
import ar.uade.edu.fain.ad.server.business.Mozo;
import ar.uade.edu.fain.ad.server.business.Salon;
import ar.uade.edu.fain.ad.server.dao.EmpleadoDAO;
import ar.uade.edu.fain.ad.server.dao.entity.AdministradorEntity;
import ar.uade.edu.fain.ad.server.dao.entity.CajeroEntity;
import ar.uade.edu.fain.ad.server.dao.entity.EmpleadoEntity;
import ar.uade.edu.fain.ad.server.dao.entity.MozoEntity;

public class EmpleadoJpaDAO extends
		GenericJpaDAO<Empleado, EmpleadoEntity, Long> implements EmpleadoDAO {

	public EmpleadoJpaDAO() {
		super(EmpleadoEntity.class);
	}

	@Override
	protected EmpleadoEntity toEntity(Empleado business) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Empleado toBusiness(EmpleadoEntity entity) {
		Empleado business = null;
		if (entity instanceof CajeroEntity) {
			business = new Cajero();
			business.setArea(new Caja());
			business.getArea().setId(entity.getArea().getId());
			business.getArea().setLocal(new Local());
			business.getArea().getLocal().setId(
					entity.getArea().getLocal().getId());
		} else if (entity instanceof AdministradorEntity) {
			final Administrador administrador = new Administrador();
			administrador.setAdministracion(new Administracion());
			administrador.getAdministracion().setId(
					((AdministradorEntity) entity).getAdministracion().getId());
			business = administrador;

		} else if (entity instanceof MozoEntity) {
			business = new Mozo();
			business.setArea(new Salon());
			business.getArea().setId(entity.getArea().getId());
			business.getArea().setLocal(new Local());
			business.getArea().getLocal().setId(
					entity.getArea().getLocal().getId());
		} else {
			throw new UnsupportedOperationException(
					entity.getClass().getCanonicalName());
		}
		business.setId(entity.getId());
		business.setNombre(entity.getNombre());
		business.setApellido(entity.getApellido());
		business.setLogin(entity.getLogin());
		business.setPassword(entity.getPassword());
		return business;
	}

	@Override
	public Empleado buscar(final String login, final String password) {
		return execute(new Query<Empleado>() {
			@Override
			public Empleado inTransaction(final Session session)
					throws PersistenceException {
				@SuppressWarnings("unchecked")
				final List<EmpleadoEntity> list = session
						.getNamedQuery("empleado.byCredenciales")
						.setParameter("login", login)
						.setParameter("pass", password)
						.list();
				return list.isEmpty() ? null : toBusiness(list.iterator()
						.next());
			}
		});
	}

}
